<?php

namespace adminApi\modules\product\controllers;

use adminApi\modules\product\models\Categories;
use common\components\fast_api\Controller;
use common\components\wtools\tools\Model;
use common\components\wtools\tools\Tree;
use common\datas\Enums;
use common\Tools;

class CategoryController extends Controller
{
    /**
     * 获取分类树
     * @desc 分类树
     * @return array
     * @return array tree
     */
    public function actionList()
    {
        $categories = Categories::find()
            ->with(['parent'])
            ->andWhere(['status' => Enums::ACTIVE])
            ->orderBy(['cate_sort' => SORT_DESC, 'id' => SORT_ASC])
            ->all();
        $list = [];
        foreach ($categories as $k => &$v) {
            $m['id'] = $v->id;
            $m['name'] = $v->name;
            $m['pid'] = $v->pid;
            $m['_created_at'] = date('Y-m-d H:i:s', $v->created_at);
            $m['_status'] = Enums::getStatusDesc()[$v->status];
            if ($v->parent) {
                $m['pName'] = $v->parent->name;
            }
            $list[] = $m;
        }
        $tree = Tree::list2tree($list, null, 'id', 'pid', 'children');
        return $this->apiSuccess([
            'tree' => $tree,
        ]);
    }

    /**
     * 添加或编辑分类
     * @desc 添加或编辑分类
     * @param string $name
     * @param int $id
     * @param int $pid
     * @param int $cate_sort
     * @param int $status
     * @param int $is_danger
     * @param int $is_virtual
     * @return array
     */
    public function actionAddOrEdit($name, $id = 0, $pid = 0, $cate_sort = 1, $status = Enums::ACTIVE, $is_danger = Enums::NO, $is_virtual = Enums::NO)
    {
        if ($id > 0) {
            $category = Categories::findOne($id);
        } else {
            $category = new Categories();
        }
        $category->name = $name;
        $category->pid = $pid ?: null;
        $category->cate_sort = $cate_sort;
        $category->status = $status;
        $category->is_danger = $is_danger;
        $category->is_virtual = $is_virtual;
        if (!$category->save()) {
            return $this->apiError(Model::getModelError($category));
        }
        return $this->apiSuccess();
    }

    /**
     * 删除分类
     * @desc 删除末级分类
     * @param int $id
     * @return array
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDel($id)
    {
        $category = Categories::findOne($id);
        if (count($category->children) > 0) {
            return $this->apiError('该分类下有子分类，不能删除');
        }
        if (!$category->delete()) {
            return $this->apiError(Model::getModelError($category));
        }
        return $this->apiSuccess();
    }

    /**
     * 获取节点下的分类
     * @desc 在下拉选择ajax使用。使用 tree-data-simple-mode 属性来启用简单模式，这样组件会期望一个包含 id、pId、value、title 等字段的数组作为树数据
     * @param int $pid
     * @param string $chainIds
     * @return array
     * @return array list
     */
    public function actionSelect($pid = 0, $chainIds = "")
    {
        $query = Categories::find()
            ->with(['children'])
            ->andWhere(['status' => Enums::ACTIVE])
            ->orderBy(['cate_sort' => SORT_DESC, 'id' => SORT_ASC]);
        if ($pid > 0) {
            $query->andWhere(['pid' => $pid]);
        } else {
            $query->andWhere(['pid' => null]);
        }
        $ids = explode('-', $chainIds);
        if ($ids) {
            $query->orWhere(['id' => $ids]);
        }
        $categories = $query->all();
        $list = [];
        foreach ($categories as $k => &$v) {
            $m = [];
            $m['id'] = $v->id;
            $m['value'] = $v->id;
            $m['title'] = $v->name;
            $m['pId'] = $v->pid;
            $m['isLeaf'] = true;
//            $m['children'] = [];
            if ($v->children) {
                $m['isLeaf'] = false;
            }
            $list[] = $m;
        }
        return $this->apiSuccess([
            'list' => $list,
        ]);
    }
}
